package com.arrogames.blondbattle.game.movement
{
	import ash.tools.ListIteratingSystem;
	
	import com.arrogames.blondbattle.game.GameConfig;
	import com.arrogames.blondbattle.game.movement.Motion;
	import com.arrogames.blondbattle.game.movement.Position;
	import com.arrogames.blondbattle.game.movement.MovementNode;


	public class MovementSystem extends ListIteratingSystem
	{
		private var config : GameConfig;
		
		public function MovementSystem( config : GameConfig )
		{
			this.config = config;
			
			super( MovementNode, updateNode );
		}

		private function updateNode( node : MovementNode, time : Number ) : void
		{
			var position : Position = node.position;
			var motion : Motion = node.motion;

			position = node.position;
			motion = node.motion;
			position.updatePosition(motion.velocity.x * time, motion.velocity.y * time);
			position.rotation += motion.angularVelocity * time;
			if ( motion.damping > 0 )
			{
				var xDamp : Number = Math.abs( Math.cos( position.rotation ) * motion.damping * time );
				var yDamp : Number = Math.abs( Math.sin( position.rotation ) * motion.damping * time );
				if ( motion.velocity.x > xDamp )
				{
					motion.velocity.x -= xDamp;
				}
				else if ( motion.velocity.x < -xDamp )
				{
					motion.velocity.x += xDamp;
				}
				else
				{
					motion.velocity.x = 0;
				}
				if ( motion.velocity.y > yDamp )
				{
					motion.velocity.y -= yDamp;
				}
				else if ( motion.velocity.y < -yDamp )
				{
					motion.velocity.y += yDamp;
				}
				else
				{
					motion.velocity.y = 0;
				}
			}
		}
	}
}
